<?php namespace system\model;
use houdunwang\model\Model;
class Article extends Model{
	//数据表
	protected $table = "article";

	//允许填充字段
	protected $allowFill = [ '*'];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
          ['title','isnull','请填写标题',self::MUST_VALIDATE,self::MODEL_BOTH],
          ['thumb','isnull','请上传缩略图',self::MUST_VALIDATE,self::MODEL_BOTH],
          ['digest','isnull','请填写文章摘要',self::MUST_VALIDATE,self::MODEL_BOTH],
          ['author','isnull','请填写文章作者',self::MUST_VALIDATE,self::MODEL_BOTH],
          ['keywords','isnull','请填写关键字',self::MUST_VALIDATE,self::MODEL_BOTH],
          ['category_cid','isnull','请选择文章分类',self::MUST_VALIDATE,self::MODEL_BOTH],
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
          //插入事件戳，是系统函数time(),所以方法类型是function，验证是插入时验证
          ['sendtime','time','function',self::MUST_AUTO,self::MODEL_INSERT],
          //编辑文章时用更新时间
          ['updatetime','time','function',self::MUST_AUTO,self::MODEL_UPDATE],
          //getUid是我自己定义的函数，所以用method，验证时机不好确定，所以二者都，即插入和更新都
          ['user_uid','getUid','method',self::MUST_AUTO,self::MODEL_BOTH],
	];
	public function getUid($val,$data){   //框架里找
          //自动完成里getUid相当于调用，所以必须有返回值给它，session必须用框架格式，不能原生$_SESSION['uid']，因框架做了处理
        return Session::get('uid');
      }


	/**
       * 获得所有分类数据
       */
	public function getCateData(){
	    //Arr::tree把分类表category的cname字段变成树状结果
          return Arr::tree(Db::table('category')->get(),'cname');
      }
    /**
     * 获取所有标签数据
     */
      public function getTagData(){
	    //获取标签中的所有数据
          return Db::table('tag')->get();
      }
    /**
     * 分页处理，所有数据
     */
        public function getAllData($num,$is_recycle){
            //统计文章表不在回收站的数据总数,where等于可以省略,但join里的不可省略
            $count = Db::table('article')->where('is_recycle','=',$is_recycle)->count();
            //获得每页显示的内容条数是$num,总共$count条数据
            Page::row($num)->make($count);
            $page = Page::desc(['pre'=>'< <','next'=>'> >','first'=>'首页','end'=>'尾页','unit'=>'个'])->make($count,$num);
            //hdphp不支持别名。因为添加文章主页有‘所属分类’，而且必须是汉字，所以要做关联表，这是新的一张表
            $data = Db::table('article')->join('category','article.category_cid','=','category.cid')
                ->where('is_recycle',$is_recycle)->limit(Page::limit())->get();
            //p($data);
            //p($page);
            //返回分页和关联表
            return ['page'=>$page,'data'=>$data];
        }

      /**
       * 添加
       */
      public function store(){
          if(!isset($_POST['tag'])){
              return ['valid'=>0,'message'=>'请选择标签'];
          }
          //1.先看第一张表有哪些字段：添加文章表【两部分，下面第一部分+自动完成】
          //title click sendtime updatetime thumb digest author keywords is_recycle user_uid  category_cid,
          //其中sendtime user_uid在自动完成里面做,click前端做，updatetime不做，is_recycle不做，默认没有回收为0，回收为1
          //最后剩余：title  sendtime  thumb digest author keywords  user_uid  category_cid,
          $this->title = $_POST['title'];
          $this->author = $_POST['author'];
          $this->category_cid = $_POST['category_cid'];
          $this->thumb = $_POST['thumb'];
          $this->digest = $_POST['digest'];
          $this->keywords = $_POST['keywords'];
          //p($_POST);die;
          $aid = $this->save();

          //2.再看第二张表，添加文章数据表 des content article_aid(由文章表的aid获得)
          $articleDataModel = new ArticleData();
          $articleDataModel->des = $_POST['des'];
          $articleDataModel->content = $_POST['content'];
          $articleDataModel->article_aid = $aid;
          $articleDataModel->save();

          //3.添加文章标签中间表,有两个字段
          //article_aid tag_tid
          //1   10
          //1   11
          //1   12
          $articleTagModel = new ArticleTag();
          //做判断，验证文章标签必须得选择，判断放在了最上面
          foreach($_POST['tag'] as $v){
              $articleTagModel->article_aid = $aid;
              $articleTagModel->tag_tid = $v;
              $articleTagModel->save();
          }
            return ['valid'=>1,'message'=>'操作成功'];
      }

        /**
         * 编辑
         */
        public function getOldData($aid){
            //get()的结果是二维数组，first是一维。
            $s = Db::table('article')->join('article_data','article.aid','=','article_data.article_aid')
            ->where('is_recycle',0)->where('article.aid','=',$aid)->first();
            return $s;
        }
        public function edit(){
            if(!isset($_POST['tag'])){
                return ['valid'=>0,'message'=>'请选择标签'];
            }
            $aid = $_POST['aid'];
            //文章表修改
            $articleModel = $this->find($aid);//和添加不同的就是这个，先找到某一条再save就是更新
            $articleModel->title = $_POST['title'];
            $articleModel->author = $_POST['author'];
            $articleModel->category_cid = $_POST['category_cid'];
            $articleModel->thumb = $_POST['thumb'];
            $articleModel->digest = $_POST['digest'];
            $articleModel->keywords = $_POST['keywords'];
            $articleModel->save();

//            //2.文章数据表修改
//            //根据要编辑的文章id获得文章数据表对应的主键ad_id
            $ad_id = Db::table('article_data')->where('article_aid',$aid)->pluck('ad_id');//获得一个，lists是获得一列
            $articleDataModel = ArticleData::find($ad_id);//映射，找到主键$ad_id的所有数据
            $articleDataModel->des = $_POST['des'];
            $articleDataModel->content = $_POST['content'];
            $articleDataModel->article_aid = $aid;
            $articleDataModel->save();
//
//            //3.文章标签中间表的修改,因为是多对多，修改比较难，所以先删除，在新增【删除+新增】
            $articleTagModel = new ArticleTag();
            //1)在文章标签中间表中，删除原来当前文章的相关数据
            ArticleTag::where('article_aid',$aid)->delete();//手册上是News::delete(8)
            //2)新增
            //做判断，验证文章标签必须得选，判断放在最上面
            foreach($_POST['tag'] as $v){
                $articleTagModel->article_aid = $aid;
                $articleTagModel->tag_tid =$v;
                $articleTagModel->save();
            }
            return ['valid'=>1,'message'=>'操作成功'];
        }

        /*
         * 彻底删除
         */
        public function AbsoluteDel($aid){
            //删除文章表
            $this->where('aid',$aid)->delete();
            //删除文章数据表
            ArticleData::where('article_aid','=',$aid)->delete();
            //删除文章标签中间表
            ArticleTag::where('article_aid',$aid)->delete();
            return 1;
        }
}





































